package aam.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import aam.model.BoardListVO;
import aam.model.PagingBean;
import aam.model.board.BoardDao;
import aam.model.board.BoardVO;
import aam.model.member.MemberVO;

@Service
public class BoardServiceImpl implements BoardService {
	@Resource(name="boardDaoImpl")
	private BoardDao boardDao;
	
	@Override
	public BoardListVO getAllView(String page ,int boardNo, boolean genreFlag) throws Exception {
		ArrayList<BoardVO> list = null;
		int p = 1;
		HashMap map = new HashMap();
		map.put("boardNo", boardNo);
		
		if(page!=null){
			p = Integer.parseInt(page);
		}
		map.put("p", p);
		
		if(genreFlag==true){
			list = (ArrayList<BoardVO>) boardDao.getAllViewWithGenre(map);
		}else{
			list = (ArrayList<BoardVO>) boardDao.getAllView(map);
		}
		PagingBean pb = new PagingBean(p, boardDao.getBoardTotalCount(boardNo));
		return new BoardListVO(list, pb);
	}
	@Override
	public List<String> getGenreList() throws Exception {
		return boardDao.getGenreList();
	}

	@Override
	public String getBoardNameByBoardNo(int boardNo) throws Exception {
		return boardDao.getBoardNameByBoardNo(boardNo);
	}
	
	@Override
	public BoardVO getContentView(int no) throws Exception {
		boardDao.updateCount(no);
		return boardDao.getContentView(no);
	}
	
	@Override
	public BoardVO getContentNo(int no) throws Exception {
		return boardDao.getContentView(no);
	}

	@Override
	public void write(BoardVO vo) throws Exception {
		if(vo.getBoardKind().equals("20")){
			boardDao.writeFileUpload(vo);
		}else if(vo.getBoardKind().equals("21")){
			vo.setContent(" ");
			boardDao.writeImageFileUpload(vo);
		}else{
			boardDao.write(vo);
		}
		boardDao.updatePointWrite(vo.getMemberVO().getId());
	}
	
	@Override
	public void updateBoard(BoardVO bvo) throws Exception {
		if(bvo.getBoardKind().equals("20")){
			boardDao.updateBoardFileUpload(bvo);
		}else{
			boardDao.updateBoard(bvo);
		}
	}
	@Override
	public void deleteBoard(int no) throws Exception {
		boardDao.deleteBoard(no);
	}
	
	@Override
	public BoardListVO getAllViewFindBySelect(String page, int boardNo,
			boolean genreFlag, String select, String content, String genreName) throws Exception {
		ArrayList<BoardVO> list = null;
		PagingBean pb = null;
		int p = 1;
		HashMap map = new HashMap();
		map.put("boardNo", boardNo);
		if(page!=null){
			p = Integer.parseInt(page);
		}
		map.put("p", p);
		map.put("content", content);
		
		if(genreFlag==true&&!genreName.equals("All")){
			int genreNo = boardDao.getGenreNo(genreName);
			map.put("genreNo", genreNo);
			if(select.equals("title")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByGenreTitle(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectGenreTitle(map));
			}else if(select.equals("nick")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByGenreNick(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectGenreNick(map));
			}else if(select.equals("content")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByGenreContent(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectGenreContent(map));
			}	
		}else if(genreFlag==true&&genreName.equals("All")){
			if(select.equals("title")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByTitleWithGenre(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectTitleWithGenre(map));
			}else if(select.equals("nick")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByNickWithGenre(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectNickWithGenre(map));
			}else if(select.equals("content")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByContentWithGenre(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectContentWithGenre(map));
			}
		}else{
			if(select.equals("title")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByTitle(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectTitle(map));
			}else if(select.equals("nick")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByNick(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectNick(map));
			}else if(select.equals("content")){
				list = (ArrayList<BoardVO>)boardDao.getAllViewFindByContent(map);
				pb = new PagingBean(p, boardDao.getBoardTotalCountBySelectContent(map));
			}
		}
		return new BoardListVO(list, pb);
	}
	//2차 추가
	@Override
	public BoardListVO getAllViewFindByGenre(String page, int boardNo,
			String genreName) throws Exception {
		int genreNo = boardDao.getGenreNo(genreName);
		int p = 1;
		HashMap map = new HashMap();
		if(page!=null){
			p = Integer.parseInt(page);
		}
		map.put("p", p);
		map.put("genreNo", genreNo);
		map.put("boardNo", boardNo);
		ArrayList<BoardVO> list = (ArrayList<BoardVO>)boardDao.getAllViewFindByGenre(map);;
		PagingBean pb =  new PagingBean(p, boardDao.getBoardTotalCountBySelectGenre(map));
		
		return new BoardListVO(list, pb);
	}
	
	@Override
	public List<BoardVO> getAllViewMain() throws Exception {
		return boardDao.getAllViewMain();
	}
	
	@Override
	public List<BoardVO> getAllViewMainByView() throws Exception {
		return boardDao.getAllViewMainByView();
	}
	@Override
	public List<BoardVO> getViewMainByShare() throws Exception {
		return boardDao.getViewMainByShare();
	}
	//12.19 메인에게시판 
	@Override
	public List<BoardVO> getViewMainByReview() throws Exception {
		return boardDao.getViewMainByReview();
	}
	//파일다운로드시 포인트감소
	@Override
	public void updatePointFileDown(String  id) throws Exception {
		boardDao.updatePointFileDown(id);
	}
	/** 추천 비추천 */
	@Override
	public int recommend(String id, int no) throws Exception {
		HashMap map=new HashMap();
		map.put("id", id);
		map.put("no", no);
		int result=this.boardDao.checkRecommend(map);
		if(result==0){
			result=this.boardDao.recommend(map);
		}else{
			result=0;
		}
		return result;
	}
	@Override
	public int unrecommend(String id, int no) throws Exception {
		HashMap map=new HashMap();
		map.put("id", id);
		map.put("no", no);
		int result=this.boardDao.checkRecommend(map);
		if(result==0){
			result=this.boardDao.unrecommend(map);
		}else{
			result=0;
		}
		return result;
	}
	@Override
	public int recommendCount(int no) throws Exception {
		
		return this.boardDao.recommendCount(no);
	}
	@Override
	public int unrecommendCount(int no) throws Exception {
		return this.boardDao.unrecommendCount(no);
	}	
}
